/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package coms.action;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import coms.delegates.UserDelegate;
import coms.dto.User;
/**
 *
 * @author Atul
 */
public class AuthenticateAction extends ComsActionSupport{
    User user;
    private String error;

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
    public AuthenticateAction() {
        user = new User();        
    }
    
    public String execute() throws Exception {        
        UserDelegate delegate = new UserDelegate();
        User usr = delegate.validateUser(user);
        if(usr!=null)
        {
            System.out.println("login sucessfull");
            session.put("user", usr);
            System.out.println(usr.getId());
            System.out.println(usr.getUsername());
            return ActionSupport.SUCCESS;
            
        }else{
            error="Invalid Username/Password!!";
            addActionError("Invalid Username/Password!!");
            addFieldError("user","Invalid Username/Password!!");
            return "failure";
        }
    }
    
    public String logout()
    {
        System.out.println("logging out..");
        if(session.get("user")!=null)
        {
            session.remove("user");
        }
               
        return "logout";
    }
}
